package com.ruoyi.hr.service.impl.salary;


import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.mapper.EmpSalaryCalculationMapper;
import com.ruoyi.hr.mapper.SkyMsgAlarmMapper;
import com.ruoyi.hr.mapper.salary.SpecialPersonSalaryRegisterMapper;
import com.ruoyi.hr.mapper.welfare.welfarecalculationconfig.CcdBenefititemMapper;
import com.ruoyi.hr.service.salary.SpecialPersonSalaryRegisterService;
import com.ruoyi.hr.util.BaseResult;
import com.ruoyi.hr.util.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class SpecialPersonSalaryRegisterServiceImpl implements SpecialPersonSalaryRegisterService {

    @Autowired
    SpecialPersonSalaryRegisterMapper specialPersonSalaryRegisterMapper;

    @Autowired
    CcdBenefititemMapper ccdBenefititemMapper;

    @Autowired
    private EmpSalaryCalculationMapper empSalaryCalculationMapper;

    @Autowired
    SkyMsgAlarmMapper skyMsgAlarmMapper;

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findData(List<Authority> authorities, Map map, int isAdmin) {
        //拼接动态SQL
        List<String> ProList = empSalaryCalculationMapper.findObjTypeAll(map.get("pgid").toString());
        for (int i = 0; i < ProList.size(); i++) {
            ProList.set(i, ProList.get(i).replace("（", "("));
            ProList.set(i, ProList.get(i).replace("）", ")"));
        }
        map.put("pro", ProList);
        return specialPersonSalaryRegisterMapper.findData(authorities, map, isAdmin);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public BaseResult sealUp(String pgid, String term) {
        //叠加异常信息
        String msgAdd = "";
        try {
            String sql;
            String msg;//异常信息

            sql = "declare @RetVal varchar(50) exec csp_tsempfc " + pgid + "," + term + ",@RetVal output select @RetVal";
            List<Map<String, Object>> maps1 = specialPersonSalaryRegisterMapper.sealUp(sql);
            if (maps1.size() == 1) {
                //1代表有返回消息
                msg = skyMsgAlarmMapper.getMsgById(maps1.get(0).get("").toString());
                if (msg != null) {
                    msgAdd += msg + "\n";
                }
            }

        } catch (Exception e) {
            return BaseResult.build(ResultEnum.SERVER_ERROR);
        }

        if ("".equals(msgAdd)) {
            return BaseResult.build(ResultEnum.SUCCESS);
        } else {
            return BaseResult.build(19970926, msgAdd, null);
        }
    }
}
